//
//  DownloadItemView.swift
//  YoruNeko
//
//  Created by liphone on 2021/6/10.
//

import SwiftUI



struct DownloadItemView: View {
    
    
    @State var isEditing = false
    var dataModel:DownloadViewItem
    
    private var _progressView = YRNKCycleStyleProgressView(radius: 15, boldValue: 3, color: .black.withAlphaComponent(0.7),progress: 0.0)
    var progressView:YRNKCycleStyleProgressView{
        _progressView.progress = CGFloat(dataModel.progress)
        return _progressView
    }
    
    var onStateControllBtnPress:((Int)->Void)?
    var onTrashBtnPress:((Int)->Void)?
    var onItemPress:((Int)->Void)?
    
    init(with item: DownloadViewItem, onStateControllBtnPress:(((Int)->Void)?),onTrashBtnPress:((Int)->Void)?,onItemPress:((Int)->Void)?){
        self.dataModel = item;
        //dataModel.addObserver(progressView, forKeyPath: "downloadedSize", options: .new, context: nil)
        self.onStateControllBtnPress = onStateControllBtnPress
        self.onTrashBtnPress = onTrashBtnPress
        self.onItemPress = onItemPress
        
    }
    
    
    
    
    var body: some View {
        HStack{
            //group progressView and
            ZStack{
                //这里有个很奇怪的设置，感觉就像计算值传递
                progressView.frame(width:progressView.frame.width, height: progressView.frame.height, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                Group{
                    switch dataModel.state {
                    case .finished:
                        Button(action: onProgressButtonPressed){
                            Label("", systemImage: "checkmark")
                        }.foregroundColor(.black.opacity(0.8))
                    case .going:
                        Button(action: onProgressButtonPressed){
                           Label("", systemImage: "pause")
                        }.foregroundColor(.black.opacity(0.8))
                    case .paused:
                        Button(action: onProgressButtonPressed){
                            Label("", systemImage:"arrow.down.to.line")
                        }.foregroundColor(.black.opacity(0.8))
                    case .stopped:
                        Button(action: onProgressButtonPressed){
                            Label("", image: "")
                        }.foregroundColor(.black.opacity(0.8))
                    }
                }.frame(width: progressView.frame.width, height: progressView.frame.height, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/).padding(.leading,7).padding(.bottom,2)
                
                
                
            }
            
            VStack(alignment:.leading){
                Text(dataModel.title)
                Text(getSubTitleDescription()).font(.subheadline).foregroundColor(.gray)
            }
            Spacer()
            Button(
                action: {onTrashBtnPress?(dataModel.id.hashValue)},
                label: {
                Image(systemName: "trash.fill")
            }).foregroundColor(.black.opacity(0.7)).scaleEffect(x: 1.5, y: 1.5, anchor: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
        }.onTapGesture(count: /*@START_MENU_TOKEN@*/1/*@END_MENU_TOKEN@*/, perform: {
            onItemPress?(dataModel.id.hashValue)
        })
        
        
    }
    
    //进度条下的按钮
    func onProgressButtonPressed(){
        onStateControllBtnPress?(dataModel.id.hashValue)
    }
    
    private func getSubTitleDescription()->String{

        return "\(YRNKUtil.getSuitableFileSizeString(size: dataModel.downloadedSize))/\(YRNKUtil.getSuitableFileSizeString(size: dataModel.totalSize)) \(getDescription2())"
    }
    
    private func getDescription2()->String{
        
        var desc = ""
        switch dataModel.state {
    
        case .finished:
            desc = "已完成"
        case .going:
            desc = dataModel.speedDescription+"下载中"
        case .paused:
            desc = "已暂停"
        case .stopped:
            if let err = dataModel.errorMessage {
                desc = err
            }
            else {
                desc = "已停止"
            }
        }
        return desc
    }
    
    
}




